package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.entity.FlowConfig;
import com.softer.wxzj.mapper.FlowConfigMapper;
import com.softer.wxzj.mapper.FlowTypeMapper;
import com.softer.wxzj.service.FlowConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class FlowConfigServiceImpl implements FlowConfigService {

    @Autowired
    private FlowTypeMapper flowTypeMapper;
    @Autowired
    private FlowConfigMapper flowConfigMapper;

    @Override
    public CommonResult insert(FlowConfig flowConfig) {
        //检查是否符合新增条件
        int update = flowConfigMapper.isQualifiedCheck(flowConfig);
        if(update>0){
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "请确认流程顺序");
        }
        int i = flowConfigMapper.insert(flowConfig);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult update(FlowConfig flowConfig) {
        //检查是否符合更新条件
        int update = flowConfigMapper.isQualifiedCheck(flowConfig);
        if(update>0){
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "请确认流程顺序");
        }
        int i = flowConfigMapper.update(flowConfig);
        return CommonResult.ResponseResult(i);
    }


    @Override
    public CommonResult delete(String id) {
        int i = flowTypeMapper.delete(id);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public PageInfo<FlowConfig> getList(Page page, FlowConfig flowConfig) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<FlowConfig> list = flowTypeMapper.getList();
        PageInfo<FlowConfig> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
